package net.lab1024.sa.admin.module.business.flow.controller;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.flow.domain.form.FlwTaskQueryForm;
import net.lab1024.sa.admin.module.business.flow.domain.vo.FlwTaskVo;
import net.lab1024.sa.admin.module.business.flow.service.FlwInstanceService;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/flw")
public class FlwInstanceController {

    @Autowired
    private FlwInstanceService flwInstanceService;

    @Operation(summary = "发起流程 @author ADog")
    @PostMapping("/instance/startProcess")
    public ResponseDTO startProcess(@RequestParam Map<String,Object> params) {
        log.info("FlwInstanceController.startProcess：{}", params);
        flwInstanceService.startProcess(params);
        return ResponseDTO.ok();
    }

    /**
     * 查询我的代办
     * 审批人
     * 候选人
     * 候选人组
     */
    @Operation(summary = "查询我的待办 @author ADog")
    @PostMapping("/instance/queryMyTask")
    public ResponseDTO<PageResult<FlwTaskVo>> queryMyTask(@RequestBody FlwTaskQueryForm queryForm) {
        return ResponseDTO.ok(flwInstanceService.queryMyTask(queryForm));
    }

    /**
     * 审批通过
     */
    @Operation(summary = "审批通过 @author ADog")
    @GetMapping("/instance/completeTask/{id}")
    public ResponseDTO completeTask(@PathVariable String id) {
        // 接收的是流程taskId
        log.info("FlwInstanceController.completeTaskId：{}", id);
        flwInstanceService.completeTask(id);
        return ResponseDTO.ok("审批通过");
    }

    @Operation(summary = "流程拾取 @author ADog")
    @GetMapping("/instance/claimTask/{id}")
    public ResponseDTO claimTask(@PathVariable String id) {
        // 接收的是流程taskId
        log.info("FlwInstanceController.claimTask：{}", id);
        flwInstanceService.claimTask(id);
        return ResponseDTO.ok("拾取成功");
    }


    @Operation(summary = "流程归还 @author ADog")
    @GetMapping("/instance/unclaimTask/{id}")
    public ResponseDTO unclaimTask(@PathVariable String id) {
        // 接收的是流程taskId
        log.info("FlwInstanceController.unclaimTask：{}", id);
        flwInstanceService.unclaimTask(id);
        return ResponseDTO.ok("归还成功");
    }

    @GetMapping("/instance/downloadFlowActiveImg")
    public void downloadFlowActiveImg(String procId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        if(StringUtils.isBlank(procId)|| "null".equals(procId) || "undefined".equals(procId)){
            return ;
        }
        byte[] bytes = flwInstanceService.diagram(procId);
        response.setContentType("image/png");
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.flush();
    }
}
